package com.fenghuajueli.libbasecoreui.upload.xiaomi

import android.app.Application
import android.os.Build
import androidx.lifecycle.LifecycleOwner
import com.blankj.utilcode.util.GsonUtils
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.NetworkUtils
import com.fenghuajueli.libbasecoreui.upload.BaseUpload
import com.fenghuajueli.libbasecoreui.upload.Channel
import com.fenghuajueli.libbasecoreui.utils.MmkvUtils

class XiaomiUploadUtils : BaseUpload() {

    companion object {
        @JvmStatic
        val instance: XiaomiUploadUtils by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
            XiaomiUploadUtils()
        }
    }

    /**
     * 打开应用时检查
     * 首次安装
     * 次日留存
     * @param lifecycleOwner
     * @param application
     */
    fun openAction(lifecycleOwner: LifecycleOwner, application: Application) {
        lifecycleOwner.action(Channel.XIAOMI) {
            if (checkIsFirstInstall(application)) {
                MmkvUtils.save(FIRST_LAUNCH, false)
                LogUtils.d("firstInstallAction: 首次安装启动")
                uploadActiveData(application, XmDataType.APP_ACTIVE_NEW)
            } else if (checkSeconddayRetention()) {
                LogUtils.d("firstInstallAction: 次留")
                uploadActiveData(application, XmDataType.APP_RETENTION)
            }
        }
    }

    /**
     * 注册
     * 注册完3分钟内算新注册用户
     * @param needCheck 一键登录、微信登录需要传true
     * @param application
     */
    fun registerAction(
        lifecycleOwner: LifecycleOwner,
        application: Application,
        needCheck: Boolean = false
    ) {
        lifecycleOwner.action(Channel.XIAOMI) {
            if (needCheck) {
                if (checkIsRegister()) {
                    uploadActiveData(application, XmDataType.APP_REGISTER)
                }
            } else {
                uploadActiveData(application, XmDataType.APP_REGISTER)
            }
        }
    }


    /**
     * 付费
     * 7天内付费才上传
     * @param lifecycleOwner
     * @param application
     * @param payAmount
     */
    fun payAction(lifecycleOwner: LifecycleOwner, application: Application, payAmount: Double) {
        lifecycleOwner.action(Channel.XIAOMI) {
            if (checkPayWithinSevendays()) {
                uploadActiveData(application, XmDataType.APP_PAY, (payAmount * 100).toLong())
            }
        }
    }

    /**
     * vivo转化数据同步接口
     *
     * @param dataType
     * @param payAmount
     */
    private suspend fun uploadActiveData(
        application: Application,
        dataType: XmDataType,
        payAmount: Long? = null
    ) {
        application.uploadActiveData(Channel.XIAOMI) {
            val contentsMap = hashMapOf<String, Any>()
            contentsMap.apply {
                //oaid
                contentsMap["oaid"] = oaid ?: ""
                //转化类型
                contentsMap["conv_type"] = dataType.name
                //事件发生的系统时间戳
                contentsMap["conv_time"] = System.currentTimeMillis()
                //付费金额
                payAmount?.let { contentsMap["conv_weight"] = it }
                //IP地址
                contentsMap["client_ip"] = NetworkUtils.getIPAddress(true)
                //客户端ua
                contentsMap["ua"] = System.getProperty("http.agent") ?: ""

                //包名
                contentsMap["pkgName"] = application.packageName
                //商标
                contentsMap["brand"] = Build.BRAND
                //厂商
                contentsMap["manufacturer"] = Build.MANUFACTURER
                //机型
                contentsMap["model"] = Build.MODEL
            }

            GsonUtils.toJson(contentsMap)
        }
    }

}
